package com.situ.crm.controller;

import com.situ.crm.pojo.bo.SaleChanceSearchBO;
import com.situ.crm.pojo.entity.Customer;
import com.situ.crm.pojo.entity.Product;
import com.situ.crm.pojo.entity.SaleChance;
import com.situ.crm.pojo.entity.User;
import com.situ.crm.service.ICustomerService;
import com.situ.crm.service.IProductService;
import com.situ.crm.service.ISaleChanceService;
import com.situ.crm.service.IUserService;
import com.situ.crm.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.List;

@ApiIgnore
@RequestMapping("/saleChance")
@Controller
public class SaleChanceController {
	@Autowired
	private ISaleChanceService saleChanceService;
	@Autowired
	private ICustomerService customerService;
	@Autowired
	private IProductService productService;
	@Autowired
	private IUserService userService;

	@RequestMapping("/getSaleChancePage")
	public String getSaleChancePage() {
		return "/sale_chance/sale_chance_list";
	}

	// http://localhost:8080/saleChance/selectByPage?page=1&limit=10&chanceSource=电话&devResult=0
	@RequestMapping("/selectByPage")
	@ResponseBody
	public Result selectByPage(Integer page, Integer limit, SaleChanceSearchBO saleChanceSearchBO) {
		Result result = saleChanceService.selectByPage(page, limit, saleChanceSearchBO);
		return result;
	}

	@RequestMapping("/deleteById")
	@ResponseBody
	public Result deleteById(Integer id) {
		boolean isSuccess =  saleChanceService.deleteById(id);
		if (isSuccess) {
			return Result.success("删除成功");
		} else {
			return  Result.error("删除失败");
		}
	}

	@RequestMapping("/getAddPage")
	public String getAddPage(Model model) {
		List<Customer> customerList = customerService.selectAll();
		model.addAttribute("customerList", customerList);
		List<Product> productList = productService.selectAll();
		model.addAttribute("productList", productList);
		List<User> userList = userService.selectXiaoShouUser();
		model.addAttribute("userList", userList);
		return "/sale_chance/sale_chance_add";
	}

	@RequestMapping("/add")
	@ResponseBody
	public Result add(SaleChance saleChance) {
		System.out.println(saleChance);
		boolean isSuccess = saleChanceService.add(saleChance);
		if (isSuccess) {
			return Result.success("添加成功");
		} else {
			return  Result.error("添加失败");
		}
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public Result update(SaleChance saleChance) {
		System.out.println(saleChance);
		boolean isSuccess = saleChanceService.update(saleChance);
		if (isSuccess) {
			return Result.success("更新成功");
		} else {
			return  Result.error("更新失败");
		}
	}
	
	@RequestMapping("/getUpdatePage")
	public String getUpdatePage() {
		return "/saleChance/saleChance_update";
	}
	
}


